//
//  LGEvaluateDetailController.m
//  BreathTrainingTool
//
//  Created by 1234 on 2024/7/23.
//

#import "LGEvaluateDetailController.h"
#import "LGCircleProgressView.h"
@interface LGEvaluateDetailController ()
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UILabel *timeLabel;
/**   */
@property (nonatomic , strong)LGCircleProgressView *progressView;
/**   */
@property (nonatomic , strong)UILabel *scoreLabel;
/**   */
@property (nonatomic , strong)UILabel *unitLabel;
/**   */
@property (nonatomic , strong)UILabel *resultLabel;
/**   */
@property (nonatomic , strong)UIView *remarkView;
/**   */
@property (nonatomic , strong)UILabel *remarkLabel;
/**   */
@property (nonatomic , strong)NSArray *remarkArray;
/**   */
@property (nonatomic , strong)NSArray *colorArray;
/**   */
@property (nonatomic , copy)NSString *circleColor;
/**   */
@property (nonatomic , copy)NSString *result;
/**   */
@property (nonatomic , assign)CGFloat total;

@end

@implementation LGEvaluateDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.total = 1.0;
    self.title = @"评估报告";
    self.view.backgroundColor = [UIColor colorWithString:@"#F3F1FC"];
    self.colorArray = @[@"#87C700",@"#FFC601",@"#796DF5",@"#EF6661"];
    [self createSubView];
    [self requestScoreData];
    [self requestData];
}

-(void)requestScoreData{
    [RequestUtil withGET:@"brbot-scale/answer/score" parameters:@{@"scaleId":self.scaleid,@"batchId":self.batchid,@"patientId":self.patientid} header:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            NSArray *tempArray = returnData[@"data"];
            for (NSInteger i=0; i<tempArray.count; i++) {
                NSArray *tempArray2 = tempArray[i][@"variables"];
                for (NSDictionary *tempDic in tempArray2) {
                    if ([LGNSString(tempDic[@"name"]) isEqualToString:@"rv"]) {
                        self.result = LGNSString(tempDic[@"defaultValue"]);
                    }
                    if ([LGNSString(tempDic[@"name"]) isEqualToString:@"zf"]) {
                        self.score = LGNSString(tempDic[@"defaultValue"]);
                    }
                }
            }
        }
    } error:^(NSError *error) {
        
    }];
}

-(void)requestData{
    [RequestUtil withGET:@"brbot-system/dict-biz/detail" parameters:@{@"id":self.bizid} header:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            self.titleLabel.text = LGNSString(returnData[@"dictValue"]);
            NSMutableArray *remarkArray = [NSMutableArray arrayWithArray:[ObjectTool jsonToObject:returnData[@"remark"]]];
            for (NSDictionary *tempDic in remarkArray) {
                if ([tempDic.allKeys containsObject:@"desc"]) {
                    self.total = [tempDic[@"value"] floatValue]>0?[tempDic[@"value"] floatValue]:1.0;
                    [remarkArray removeObject:tempDic];
                    break;
                }
            }
            self.remarkArray = remarkArray;
        }
    } error:^(NSError *error) {
        
    }];
}

-(void)setTime:(NSString *)time{
    _time = time;
    self.timeLabel.text = time;
}

-(void)setScore:(NSString *)score{
    _score = score;
    self.scoreLabel.text = score;
}

-(void)setResult:(NSString *)result{
    _result = result;
    if (result.length>0) {
        NSString *content = [NSString stringWithFormat:@"结论：\n%@",result];
        self.resultLabel.attributedText = [content attributeString:@{NSFontAttributeName:LGFont(16)} range:NSMakeRange(0, 3)];
    }else{
        self.resultLabel.text = @"";
    }
}

-(void)setRemarkArray:(NSArray *)remarkArray{
    _remarkArray = remarkArray;
    if (remarkArray.count>0) {
        UILabel *titleLabel = [UILabel lableWithText:@"参考值" colorString:@"#2B2A37" textFont:LGFont(15) textAlignment:NSTextAlignmentLeft lines:1];
        titleLabel.frame = CGRectMake(viewPix(16), viewPix(16), viewPix(150), viewPix(20));
        [self.remarkView addSubview:titleLabel];
        
        CGFloat top = viewPix(45);
        for (NSInteger i=0; i<remarkArray.count; i++) {
            top = [self creatRemarkView:remarkArray[i] top:top index:i];
        }

        [self.remarkView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@(top+viewPix(10)));
        }];
    }else{
        [self.remarkView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@(0));
        }];
    }
}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    [self.view addSubview:self.baseView];
    [self.baseView addSubview:self.titleLabel];
    [self.baseView addSubview:self.timeLabel];
    [self.baseView addSubview:self.progressView];
    [self.baseView addSubview:self.scoreLabel];
    [self.baseView addSubview:self.unitLabel];
    [self.baseView addSubview:self.resultLabel];
    [self.baseView addSubview:self.remarkView];
    [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.view).offset(viewPix(16));
        make.right.equalTo(self.view).offset(-viewPix(16));
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.baseView).offset(viewPix(24));
        make.right.equalTo(self.baseView).offset(-viewPix(24));
    }];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(viewPix(7));
        make.centerX.equalTo(self.titleLabel);
    }];
    [self.progressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.timeLabel.mas_bottom).offset(viewPix(20));
        make.centerX.equalTo(self.titleLabel);
        make.width.height.equalTo(@(viewPix(147)));
    }];
    [self.scoreLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.equalTo(self.progressView);
    }];
    [self.unitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.scoreLabel.mas_bottom);
        make.centerX.equalTo(self.scoreLabel);
    }];
    [self.resultLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.progressView.mas_bottom).offset(viewPix(10));
        make.left.right.equalTo(self.titleLabel);
    }];
    [self.remarkView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.resultLabel.mas_bottom).offset(viewPix(16));
        make.left.equalTo(self.baseView).offset(viewPix(16));
        make.right.equalTo(self.baseView).offset(-viewPix(16));
        make.bottom.equalTo(self.baseView).offset(-viewPix(16));
        make.height.equalTo(@(0));
    }];
}

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [[UIView alloc]initWithFrame:CGRectMake(viewPix(16), viewPix(16), Screen_W-viewPix(32), viewPix(484))];
        _baseView.backgroundColor = [UIColor colorWithString:@"#FFFFFF"];
        _baseView.cornerRidus = 10;
    }
    return _baseView;
}


-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"" colorString:@"#2B2A37" textFont:LGFontWeight(18, UIFontWeightMedium) textAlignment:NSTextAlignmentCenter lines:2];
    }
    return _titleLabel;
}

-(UILabel *)timeLabel{
    if(!_timeLabel){
        _timeLabel = [UILabel lableWithText:@"" colorString:@"#909399" textFont:LGFont(16) textAlignment:NSTextAlignmentCenter lines:1];
    }
    return _timeLabel;
}

-(LGCircleProgressView *)progressView{
    if(!_progressView){
        _progressView = [[LGCircleProgressView alloc]init];
        _progressView.bounds = CGRectMake(0, 0, viewPix(147), viewPix(147));
    }
    return _progressView;
}

-(UILabel *)scoreLabel{
    if(!_scoreLabel){
        _scoreLabel = [UILabel lableWithText:@"" colorString:@"#2B2A37" textFont:LGFontWeight(20, UIFontWeightMedium) textAlignment:NSTextAlignmentCenter lines:1];
    }
    return _scoreLabel;
}

-(UILabel *)unitLabel{
    if(!_unitLabel){
        _unitLabel = [UILabel lableWithText:@"分数" colorString:@"#909399" textFont:LGFont(16) textAlignment:NSTextAlignmentCenter lines:1];
    }
    return _unitLabel;
}

-(UILabel *)resultLabel{
    if(!_resultLabel){
        _resultLabel = [UILabel lableWithText:@"" colorString:@"#2B2A37" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:0];
    }
    return _resultLabel;
}

-(UIView *)remarkView{
    if(!_remarkView){
        _remarkView = [[UIView alloc]init];
        _remarkView.backgroundColor = [UIColor colorWithString:@"#F3F1FC"];
        _remarkView.cornerRidus = 5;
    }
    return _remarkView;
}

-(CGFloat)creatRemarkView:(NSDictionary *)dataDic  top:(CGFloat)top index:(NSInteger)index{
    NSString *min = LGNSString(dataDic[@"min"]);
    NSString *max = LGNSString(dataDic[@"max"]);

    if ((![min isEqualToString:max] && [self.score floatValue]>[min floatValue] && [self.score floatValue]<[max floatValue]) || ([min isEqualToString:max] && [self.score floatValue]>=[max floatValue])) {
        NSString *color = (index<self.colorArray.count)?self.colorArray[index]:@"#EF6661";
        [self.progressView addCircleWithColor:[UIColor colorWithString:color]];
        [self.progressView animateToProgress:[self.score floatValue]/self.total];
    }
    
    NSString *title = [min isEqualToString:max]?[NSString stringWithFormat:@"≥%@分者为",min]:[NSString stringWithFormat:@"%@-%@分者为",min,max];
    NSString *content = [NSString stringWithFormat:@"“%@”",dataDic[@"text"]];
    UILabel *titleLabel = [UILabel lableWithText:title colorString:@"#61616D" textFont:LGFont(13) textAlignment:NSTextAlignmentLeft lines:1];
    [self.remarkView addSubview:titleLabel];
    
    UILabel *contentLabel = [UILabel lableWithText:content colorString:@"#2B2A37" textFont:LGFont(13) textAlignment:NSTextAlignmentLeft lines:0];
    [self.remarkView addSubview:contentLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.remarkView).offset(top);
        make.left.equalTo(self.remarkView).offset(viewPix(16));
    }];
    [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.remarkView).offset(viewPix(100));
        make.right.equalTo(self.remarkView).offset(-viewPix(10));
        make.top.equalTo(titleLabel);
    }];
    CGFloat contentH = [content textHeightWithFont:LGFont(13) maxWidth:Screen_W-viewPix(174)];
    top += contentH+viewPix(10);
    return top;
}

@end
